/**
 * GIS的一些方法
 */
/**
 * 1.添加kml
 * @Fuction
 * @param {String} url KML的url地址
 * @param {Object} [styleOptions] 配置选项
 * @param {String} [styleOptions.color] KML线的颜色，默认#ff0000
 */
export function addKML(url, styleOptions = {}, options) {
  if (!url) {
    return '';
  }
  // eslint-disable-next-line no-undef
  const kmlLayer = new nwh3d.layer.KmlLayer(
    {
      name: 'name',
      url,
      // popup: "all",
      symbol: {
        styleOptions: {
          lineType: 'solid',
          clampToGround: true,
          width: 2,
          classificationType: Cesium.ClassificationType.TERRAIN,
          opacity: 0.3,
          label: {
            // 标记文字
            text: '{name}',
            opacity: 1,
            font_size: 30,
            color: '#ffffff',

            font_family: '楷体',
            border: true,
            border_color: '#000000',
            border_width: 3,

            scaleByDistance: true,
            scaleByDistance_far: 200000,
            scaleByDistance_farValue: 0.1,
            scaleByDistance_near: 1000,
            scaleByDistance_nearValue: 1,
          },
          ...styleOptions,
        },
      },
      // "center": { "y": 41.893703, "x": 112.40279, "z": 0, "heading": 359, "pitch": -79.8, "roll": 0.1 },
      visible: true,
      flyTo: false, // true,
      ...options,
      // eslint-disable-next-line no-undef
    },
    viewer,
  );
  return kmlLayer;
}

/**
 * 2.批量添加kml
 * @param {Array} list kml列表
 */
export function addAllKml(list, styleOptions, option = {}) {
  console.log({ list, styleOptions, option });
  if (!Array.isArray(list)) {
    return [];
  }
  list.forEach((item) => {
    if (item.url) {
      // kml背景色
      let color = null;
      try {
        color = JSON.parse(item.color);
      } catch (error) {
        color = { r: 255, g: 0, b: 0, a: 1 };
        console.error(error);
      }

      // kml描边
      let borderColor = null;
      try {
        borderColor = JSON.parse(item.borderColor || item.color);
      } catch (error) {
        borderColor = { r: 255, g: 0, b: 0, a: 1 };
        console.error(error);
      }

      const kml = addKML(
        item.url,
        {
          ...styleOptions,
          data: item,
          opacity: color.a,
          color: `rgb(${color.r},${color.g},${color.b})`,

          outlineColor: `rgb(${borderColor.r},${borderColor.g},${borderColor.b})`, // 边框颜色
          outlineOpacity: borderColor.a, // 边框透明度
          outlineWidth: 2, // 边框宽度
        },
        option,
      );
      if (kml) {
        // eslint-disable-next-line no-param-reassign
        item.KmlLayer = kml;
      }
    }
  });
  return list;
}
